package com.vf.admin.common.jiami;

import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.utils.pay.ApiAplayWeChat.PayUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * 校验客户端参数 是否被篡改
 */
public class JiamiUtils extends BaseController {
    private final static Logger LOGGER = LoggerFactory.getLogger(JiamiUtils.class);
    private static final String signKeyRe = "x5df2b4x5s32c5f6";

    /**
     * signKey
     */
    public Result apiGetCode(String signKey, HttpServletRequest request) {
        try {
            String timeStamp = request.getHeader("timeStamp");
            String signature = request.getHeader("signature");
            if (timeStamp==null || timeStamp.length()<=0) {
                result.setCode(300);
                result.setMsg("时间戳未传递");
                return result;
            }
            if (signature==null || signature.length()<=0) {
                result.setCode(300);
                result.setMsg("签名未传递");
                return result;
            }
            if (!apiSignCreate.signatureVerification(timeStamp, signature)) {
                result.setCode(1005);
                result.setMsg("请求错误");
                return result;
            }
            if (signKey==null || signKey.length()<=0) {
                result.setCode(313);
                result.setMsg("请求错误,请检查参数");
                return result;
            }

            Map<String, String> packageParams = new HashMap<String, String>(16);
            packageParams.put("aa","1");
            packageParams.put("aa","2");
            packageParams.put("aa","3");
            packageParams = PayUtil.paraFilter(packageParams);
            // 把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
            String prestr = PayUtil.createLinkString(packageParams);
            String mysign = PayUtil.sign(prestr, signKeyRe, "utf-8").toUpperCase();
            if (!signKey.equals(mysign)) {
                result.setCode(313);
                result.setMsg("请求错误,请检查参数");
                return result;
            }
            return null;
        } catch (Exception e) {
            LOGGER.error("异常:", e);
            return null;
        }
    }

}

